<!-- 文件上传组件 -->
<template>
  <div>
    <el-upload v-model:file-list="fileList" :class="props.showUploadBtn ? 'show-upload-btn' : 'hide-upload-btn'"
      :style="props.style" multiple :headers="props.headers" :data="props.data" :name="props.name"
      :before-upload="handleBeforeUpload" :on-remove="handleRemove" :on-progress="handleProgress"
      :on-success="handleSuccessFile" :on-error="handleError" :action="props.action" :accept="props.accept"
      :limit="props.limit" :http-request="customRequest">
      <el-button v-if="props.showUploadBtn" type="primary" :disabled="fileList.length >= props.limit">
        {{ props.uploadBtnText }}
      </el-button>
      <template v-if="props.showTip" #tip>
        <div class="el-upload__tip">
          {{ props.tip }}
        </div>
      </template>
      <template #file="{ file }">
        <div class="el-upload-list__item-info">
          <a class="el-upload-list__item-name" @click="downloadFile(file)">
            <el-icon>
              <Document />
            </el-icon>
            <span class="el-upload-list__item-file-name">{{ file.name }}</span>
            <span v-if="props.showDelBtn" class="el-icon--close" @click.stop="handleRemove(file)">
              <el-icon>
                <Close />
              </el-icon>
            </span>
          </a>
        </div>
      </template>
    </el-upload>
    <el-progress v-if="showUploadPercent" :style="{
      display: showUploadPercent ? 'inline-flex' : 'none',
      width: '100%',
    }" :percentage="uploadPercent" :color="customColorMethod" />
  </div>
</template>
<script lang="ts" setup>
import {
  UploadFile,
  UploadProgressEvent,
  UploadRawFile,
  UploadUserFile
} from "element-plus";

import FileAPI from "@/api/file";
import { getToken } from "@/utils/auth";
import { MD5, lib } from "crypto-js";

const emit = defineEmits(["update:modelValue"]);
const props = defineProps({
  /**
   * 文件集合
   */
  modelValue: {
    type: Array<UploadUserFile>,
    default: () => [],
  },
  /**
   * 上传地址
   */
  action: {
    type: String,
    default: FileAPI.uploadUrl,
  },
  /**
   * 文件上传数量限制
   */
  limit: {
    type: Number,
    default: 10,
  },
  /**
   * 是否显示删除按钮
   */
  showDelBtn: {
    type: Boolean,
    default: true,
  },
  /**
   * 是否显示上传按钮
   */
  showUploadBtn: {
    type: Boolean,
    default: true,
  },
  /**
   * 单个文件上传大小限制(单位MB)
   */
  maxSize: {
    type: Number,
    default: 2 * 1024 * 1024,
  },
  /**
   * 上传文件类型
   */
  accept: {
    type: String,
    default: "*",
  },
  /**
   * 上传按钮文本
   */
  uploadBtnText: {
    type: String,
    default: "上传文件",
  },
  /**
   * 是否展示提示信息
   */
  showTip: {
    type: Boolean,
    default: false,
  },
  /**
   * 提示信息内容
   */
  tip: {
    type: String,
    default: "",
  },
  /**
   * 请求头
   */
  headers: {
    type: Object,
    default: () => {
      return {
        Authorization: getToken(),
      };
    },
  },
  /**
   * 请求携带的额外参数
   */
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  /**
   * 上传文件的参数名
   */
  name: {
    type: String,
    default: "file",
  },
  /**
   * 样式
   */
  style: {
    type: Object,
    default: () => {
      return {
        width: "300px",
      };
    },
  },
});

const fileList = ref([] as UploadUserFile[]);
const valFileList = ref([] as UploadUserFile[]);
const showUploadPercent = ref(false);
const uploadPercent = ref(0);

watch(
  () => props.modelValue,
  (newVal: UploadUserFile[]) => {
    const filePaths = fileList.value.map((file) => file.url);
    const fileNames = fileList.value.map((file) => file.name);
    // 监听modelValue文件集合值未变化时，跳过赋值
    if (
      filePaths.length > 0 &&
      filePaths.length === newVal.length &&
      filePaths.every((x) => newVal.some((y) => y.url === x)) &&
      newVal.every((y) => filePaths.some((x) => x === y.url)) &&
      fileNames.every((x) => newVal.some((y) => y.name === x)) &&
      newVal.every((y) => fileNames.some((x) => x === y.name))
    ) {
      return;
    }

    if (newVal.length <= 0) {
      fileList.value = [];
      valFileList.value = [];
      return;
    }

    fileList.value = newVal.map((file) => {
      return { name: file.name, url: file.url } as UploadFile;
    });

    valFileList.value = newVal.map((file) => {
      return { name: file.name, url: file.url } as UploadFile;
    });
  },
  { immediate: true }
);


// 添加一个ref来引用el-upload组件
const uploadRef = ref();

const customRequest = (file: any) => {
  return new Promise((resolve, reject) => {
    FileAPI.chunkReady({ filename: file.file.name }).then((res: any) => {
      let uk = res.uk;
      chunkFile(file).then((chunkList: any) => {
        let promiseList = [];
        for (let i: any = 0; i < chunkList.length; i++) {
          promiseList.push(
            new Promise((resolve2, reject2) => {
              const formData = new FormData();
              formData.append("file", chunkList[i].content);
              formData.append("uk", uk);
              formData.append("total", chunkList.length);
              formData.append("idx", i + 1);
              formData.append("md5", chunkList[i].md5);

              FileAPI.chunk(formData).then((step2Res: any) => {
                resolve2(step2Res);
              });
            })
          );
        }

        // 上传所有分片
        Promise.all(promiseList)
          .then((resList) => {
            // 上传的结果里面只会有一个请求返回成功

            let resData = null;
            resList.forEach((item: any) => {
              if (item.url !== undefined) {
                resData = item;
              }
            });

            if (resData === null) {
              reject(new Error("返回数据有误"));
            } else {
              resolve(resData);
            }
          })
          .catch((err) => {
            reject(err);
          });
      })
    })
  })
}


/**
     * 对文件进行分片
     * @param {File} file
     */
const chunkFile = ({ file }: any) => {
  // 每片的大小
  let chunkSize = 1 * 1024 * 1024;
  let cnt = Math.ceil(file.size / chunkSize)
  let list: any = {
    length: cnt,
  };
  let pList = [];

  for (let i = 0; i < cnt; i++) {

    pList.push(new Promise<void>((resolve, reject) => {
      let tmp: any = '';
      let start = i * chunkSize;
      let end = Math.min(file.size, start + chunkSize);

      // 切割文件
      tmp = file.slice(start, end);
      let reader = new FileReader();
      reader.onload = function (ev: any) {

        // 每个片里面装的是 base64编码后的内容
        // 去掉 base64,前面的的内容
        let p: any = ev.target.result;
        // let p = '';
        // if (base64Str.indexOf('base64,') > -1) {
        //     p = base64Str.split('base64,')[1];
        // } else {
        //     p = base64Str;
        // }
        // p = str.safeEncodeURL(p);
        // 这里因为 onload是异步执行，所以用push()会导致乱序
        list[i] = {
          content: tmp,
          md5: MD5(lib.WordArray.create(p)).toString()
        };
        resolve()
      }
      reader.onerror = function (er) {
        reject(er)
      }
      // reader.readAsDataURL(tmp)
      reader.readAsArrayBuffer(tmp)

    }))

  }

  return new Promise((res, rej) => {
    Promise.all(pList).then(() => {
      res(list)
    })
      .catch(err => {
        rej(err);
      })
  })
}

/**
 * 限制用户上传文件的大小
 */
function handleBeforeUpload(file: UploadRawFile) {
  if (file.size > props.maxSize) {
    ElMessage.warning("上传文件不能大于" + props.maxSize + "M");
    return false;
  }
  uploadPercent.value = 0;
  showUploadPercent.value = true;
  return true;
}

const handleSuccessFile = (response: any, file: UploadFile) => {
  showUploadPercent.value = false;
  uploadPercent.value = 0;
  ElMessage.success("上传成功");
  valFileList.value.push({
    name: file.name,
    url: response.url,
  });
  emit("update:modelValue", valFileList.value);
  return;
};

const handleError = (error: any) => {
  showUploadPercent.value = false;
  uploadPercent.value = 0;
  ElMessage.error("上传失败");
};

const customColorMethod = (percentage: number) => {
  if (percentage < 30) {
    return "#909399";
  }
  if (percentage < 70) {
    return "#375ee8";
  }
  return "#67c23a";
};

const handleProgress = (event: UploadProgressEvent) => {
  uploadPercent.value = event.percent;
};

/**
 * 删除文件
 */
function handleRemove(removeFile: UploadUserFile) {
  const filePath = removeFile.url;
  if (filePath) {
    // 删除成功回调
    valFileList.value = valFileList.value.filter((file) => file.url !== filePath);
    emit("update:modelValue", valFileList.value);
  }
}

/**
 * 下载文件
 */
function downloadFile(file: UploadUserFile) {
  const filePath = file.url;
  if (filePath) {
    FileAPI.downloadFile(filePath, file.name);
  }
}
</script>
<style lang="scss" scoped>
.el-upload-list__item .el-icon--close {
  position: absolute;
  top: 50%;
  right: 5px;
  color: var(--el-text-color-regular);
  cursor: pointer;
  opacity: 0.75;
  transition: opacity var(--el-transition-duration);
  transform: translateY(-50%);
}

:deep(.el-upload-list) {
  margin: 0;
}

:deep(.el-upload-list__item) {
  margin: 0;
}

.show-upload-btn {
  :deep(.el-upload) {
    display: inline-flex;
  }
}

.hide-upload-btn {
  :deep(.el-upload) {
    display: none;
  }
}
</style>
